/*
 * Created by JFormDesigner on Thu Jun 27 08:52:08 CST 2024
 */

package scct.hys.ui;

import scct.hys.dao.CinemaDao;
import scct.hys.dao.UserDao;
import scct.hys.model.User;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Vector;
import javax.swing.*;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

/**
 * @author 26984
 */
public class PanelUserManger extends JPanel {

    DefaultTableModel Searchmodel=null;

    DefaultTableModel model;
    int id;
    int User;
    FormMain main;
    public PanelUserManger(FormMain main) {
        this.main=main;
        initComponents();
        initView();//定义初始化方法
        this.id=id;
    }

    private void initView(){
//        自己初始化
//        获取表格对象的数据模型，通过对模型的操作，为表格添加和实现数据
        model = (DefaultTableModel) tabUser.getModel();//alt+enter
        model.addColumn("id");
        model.addColumn("用户名");
        model.addColumn("密码");
        model.addColumn("性别");
        model.addColumn("年龄");
        model.addColumn("手机号码");

        getTable();
//
//      UserDao userDao=new UserDao();
//        List<User> allUser=userDao.getAllUser();
//        for (User user:allUser){
//            txtSearchKey.addItem(user.getName());
//
//        }
    }
    private void getTable() {

        model.setRowCount(0);
        List<User> allUser=UserDao.getAllUser();
        for(User user:allUser){
            System.out.println(user);
            model.addRow(new Object[]  {user.getId(), user.getName(),
                    user.getPassword(), user.getGender(),user.getAge(), user.getMobile()});
        }

    }
    private void btnAdd(ActionEvent e) {
            // TODO add your code here
        DlgUserAdd dlgUserAdd=new DlgUserAdd(main);
//    设置为模态对话框
        dlgUserAdd.setModal(true);
        dlgUserAdd.setVisible(true);
        getTable();
    }

    private void btnupdate(ActionEvent e) {
        // TODO add your code here
        if (tabUser.getSelectedRowCount()==0){
            JOptionPane.showMessageDialog(this,"请选择修改对象");
            return;
        }
        int index=tabUser.getSelectedRow();
        int id=Integer.parseInt(model.getValueAt(index,0).toString());
        System.out.println(id);
        DlgUserUpdate dlgUserUpdate = new DlgUserUpdate(main, id);
        dlgUserUpdate.setModal(true);
        dlgUserUpdate.setVisible(true);
        getTable();
    }

    private void btnDel(ActionEvent e) {
        // TODO add your code here
       if (tabUser.getSelectedRowCount()==0){
           JOptionPane.showMessageDialog(this,"请选择要删除的用户");
           return;
       }
       int index=tabUser.getSelectedRow();
        System.out.println(index);
        int id=Integer.parseInt(model.getValueAt(index,0).toString());
        DlgUserDel dlgUserDel=new DlgUserDel(main,id);
        dlgUserDel.setModal(true);
        dlgUserDel.setVisible(true);
        getTable();


    }

    private void cboUser(ActionEvent e) {
//        // TODO add your code here
//        UserDao userDao=new UserDao();
//        List<User> userList=new ArrayList<>();
//        int

    }

    private void btnSearch(ActionEvent e) throws SQLException {
//        // TODO add your code here
//
        System.out.println(e.getSource());
        tabUser.setModel(model);
        Searchmodel=new DefaultTableModel();
        Searchmodel.addColumn("id");
        Searchmodel.addColumn("用户名");
        Searchmodel.addColumn("密码");
        Searchmodel.addColumn("性别");
        Searchmodel.addColumn("年龄");
        Searchmodel.addColumn("手机号码");
        Searchmodel.setRowCount(0);
        String searchkey=txtsearchkey.getText();
        System.out.println(searchkey);
        String searchlist= (String) cboSearch.getSelectedItem();
        System.out.println(searchlist);
        int row=tabUser.getRowCount();
        System.out.println("row"+row);
        int column=tabUser.getColumnCount();
        System.out.println("column"+column);
        for (int i=0;i<row;i++){
//            if(tabUser.getRowCount()<row){
//                break;
//            }else {
                for (int j=0;j<column;j++){
                    if(searchlist.equals(tabUser.getColumnName(j))){
                        System.out.println("单元格的值:"+tabUser.getValueAt(i,j));
                        String cellvalue=tabUser.getValueAt(i,j).toString().toLowerCase();
                        if(cellvalue.contains(searchkey)){
                            Object[] rowData=new Object[tabUser.getColumnCount()];
                            for(int k=0;k<tabUser.getColumnCount();k++){
                                rowData[k]=tabUser.getValueAt(i,k);
                            }
                            Searchmodel.addRow(rowData);

                            tabUser.setRowSelectionAllowed(true);
                            break;
                        }
                        tabUser.setModel(Searchmodel);
                    }

            }
        }

//          String[]arr=new String[]{"id","name","gender","age","mobile"};
//          int id=txtSearchKey.getSelectedIndex();
//          String name=txtsearch.getText();
//          List<User> list=UserDao.search(id,name);
//          getTable();
//
//        String text= txtsearch.getText();
//        ArrayList<User> users=new UserDao().getUserByName(text);
//        model.setRowCount(0);
//        for (User user:users){
//            Vector v=new Vector();
//            v.add(user.getId());
//            v.add(user.getName());
//            v.add(user.getPassword());
//            v.add(user.getGender());
//            v.add(user.getAge());
//            v.add(user.getMobile());
//            model.addRow(v);
//        }
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - 韩月生
        btnPanel = new JPanel();
        btnAdd = new JButton();
        btnDel = new JButton();
        btnupdate = new JButton();
        cboSearch = new JComboBox<>();
        txtsearchkey = new JTextField();
        btnSearch = new JButton();
        scrollPane1 = new JScrollPane();
        tabUser = new JTable();

        //======== this ========
        setBorder (new javax. swing. border. CompoundBorder( new javax .swing .border .TitledBorder (new javax. swing. border. EmptyBorder(
        0, 0, 0, 0) , "JF\u006frm\u0044es\u0069gn\u0065r \u0045va\u006cua\u0074io\u006e", javax. swing. border. TitledBorder. CENTER, javax. swing. border. TitledBorder
        . BOTTOM, new java .awt .Font ("D\u0069al\u006fg" ,java .awt .Font .BOLD ,12 ), java. awt. Color.
        red) , getBorder( )) );  addPropertyChangeListener (new java. beans. PropertyChangeListener( ){ @Override public void propertyChange (java .
        beans .PropertyChangeEvent e) {if ("\u0062or\u0064er" .equals (e .getPropertyName () )) throw new RuntimeException( ); }} );
        setLayout(new BorderLayout());

        //======== btnPanel ========
        {
            btnPanel.setBackground(Color.white);
            btnPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 25, 5));

            //---- btnAdd ----
            btnAdd.setText("\u589e\u52a0\u7528\u6237");
            btnAdd.addActionListener(e -> btnAdd(e));
            btnPanel.add(btnAdd);

            //---- btnDel ----
            btnDel.setText("\u5220\u9664");
            btnDel.addActionListener(e -> btnDel(e));
            btnPanel.add(btnDel);

            //---- btnupdate ----
            btnupdate.setText("\u4fee\u6539");
            btnupdate.addActionListener(e -> btnupdate(e));
            btnPanel.add(btnupdate);

            //---- cboSearch ----
            cboSearch.setModel(new DefaultComboBoxModel<>(new String[] {
                "id",
                "\u7528\u6237\u540d",
                "\u624b\u673a\u53f7",
                "\u5e74\u9f84",
                "\u6027\u522b"
            }));
            btnPanel.add(cboSearch);

            //---- txtsearchkey ----
            txtsearchkey.setColumns(7);
            btnPanel.add(txtsearchkey);

            //---- btnSearch ----
            btnSearch.setText("\u67e5\u627e");
            btnSearch.addActionListener(e -> {try {
btnSearch(e);} catch (SQLException ex) {
    throw new RuntimeException(ex);
}});
            btnPanel.add(btnSearch);
        }
        add(btnPanel, BorderLayout.NORTH);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(tabUser);
        }
        add(scrollPane1, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - 韩月生
    private JPanel btnPanel;
    private JButton btnAdd;
    private JButton btnDel;
    private JButton btnupdate;
    private JComboBox<String> cboSearch;
    private JTextField txtsearchkey;
    private JButton btnSearch;
    private JScrollPane scrollPane1;
    private JTable tabUser;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
